package com.vm.osm.business.controller;

import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.common.ErrCode;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.req.ReqAddEquType;
import com.vm.osm.business.dto.req.ReqUpdateEquType;
import com.vm.osm.business.dto.rsp.RspEquTypeDetail;
import com.vm.osm.business.dto.rsp.RspEquTypePage;
import com.vm.osm.business.dto.rsp.RspError;
import com.vm.osm.business.entity.EamEquType;
import com.vm.osm.business.service.IEquTypeService;
import com.vm.osm.business.util.UuidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/** 企业设备类型
 * Created by zhangshilin on 2018/1/7.
 */
//@RestController
//@RequestMapping("/v1.0")
//@Api(value = "EquTypeController",description = "设备类型相关",tags = {"EquTypeController"})
public class EquTypeController {

    private Logger LOG = Logger.getLogger(this.getClass());

    @Autowired
    private IEquTypeService equTypeService;

    /**
     * 获取设备类型树形信息
     * @return
     */
    @ApiOperation(value = "获取设备类型树形信息",notes = "获取设备类型树形信息")
    @RequestMapping(value = "/equ_type_trees",method = RequestMethod.GET)
    public List<RspEquTypePage> getEquTypeTree(){
        LOG.info("获取设备类型列表信息");
        List<RspEquTypePage> equTypePageList = equTypeService.findPage("1");
        return equTypePageList;
    }

    /**
     * 新增设备类型
     * @return
     */
    @ApiOperation(value = "新增设备类型",notes = "新增设备类型")
    @ApiImplicitParam(name = "reqAddEquType",value = "新增设备类型请求参数",required = true,dataType = "ReqAddEquType")
    @RequestMapping(value = "/equ_types",method = RequestMethod.POST)
    public void addEquType(@RequestBody ReqAddEquType reqAddEquType){
        LOG.info("新增设备类型信息");
        EamEquType eamEquType = new EamEquType();
        eamEquType.setId(UuidUtil.getRandomUuid());
        eamEquType.setTypeName(reqAddEquType.getType_name());
        eamEquType.setTypeCode(reqAddEquType.getType_code());
        eamEquType.setParentId(reqAddEquType.getParent_id());
        eamEquType.setModifyedTime(new Date());
        eamEquType.setEnterpriseId("1");
        int res = equTypeService.insertSelective(eamEquType);
        if (res == 0){
            throw new BusinessException(ErrCode.ADD_DATA_FAIL.toString());
        }
    }

    /**
     * 修改设备类型
     * @param reqUpdateEquType
     * @return
     */
    @ApiOperation(value = "修改设备类型信息",notes = "修改设备类型信息")
    @ApiImplicitParam(name = "reqUpdateEquType",value = "更新设备类型请求参数",required = true,dataType = "ReqUpdateEquType")
    @RequestMapping(value = "/equ_types",method = RequestMethod.PUT)
    public void updateEquTypes(@RequestBody ReqUpdateEquType reqUpdateEquType){
        LOG.info("修改设备类型信息");
        EamEquType eamEquType = equTypeService.selectByPrimaryKey(reqUpdateEquType.getId());
        if (eamEquType == null){
            throw new BusinessException(ErrCode.DATA_NOT_FOUND.toString());
        }
        eamEquType.setTypeName(reqUpdateEquType.getType_name());
        eamEquType.setTypeCode(reqUpdateEquType.getType_code());
        int res = equTypeService.updateByPrimaryKeySelective(eamEquType);
        if (res == 0){
            throw new BusinessException(ErrCode.UPDATE_DATA_FAIL.toString());
        }
    }

    /**
     * 删除设备类型信息
     * @param id
     * @return
     */
    @ApiOperation(value = "删除设备类型信息",notes = "删除设备类型信息")
    @RequestMapping(value = "/equ_types/{id}",method = RequestMethod.DELETE)
    public void deleteEquType(@ApiParam(required=true, name="id", value="系统编号") @PathVariable("id") String id){
        LOG.info("删除设备类型信息");
        RspError rspError = new RspError();
        int res = equTypeService.deleteByPrimaryKey(id);
        if (res ==0){
            throw new BusinessException(ErrCode.DELETE_DATA_FAIL.toString());
        }
    }

    /**
     * 获取设备类型信息
     * @param id
     * @return
     */
    @ApiOperation(value = "获取设备类型信息",notes = "获取设备类型信息")
    @RequestMapping(value = "/equ_types/{id}",method = RequestMethod.GET)
    public RspEquTypeDetail getDetail(@ApiParam(required=true, name="id", value="系统编号") @PathVariable("id") String id){
        LOG.info("获取设备类型信息");
        RspEquTypeDetail rspEquTypeDetail = equTypeService.findDetail(id);
        return rspEquTypeDetail;
    }


}
